/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.dinamond;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.GLU;
import android.opengl.GLUtils;
import android.os.SystemClock;
import android.util.Log;

import com.android.test.FPS.FPSHandler;
import com.canvasgames.texture.TestTextureManager;

public class TriangleRenderer implements GLSurfaceView.Renderer{

    private FPSHandler mPFpsHandler;
	private SimplePlane simplePlane;
	private Square square;
	private TestTextureManager testTextureManager;
	public TriangleRenderer(Context context) {
        mContext = context;
        mTriangle = new Triangle();
        mPFpsHandler = new FPSHandler();
        simplePlane = new SimplePlane();
        square = new Square();
        testTextureManager = new TestTextureManager(context);
        Thread thread = new Thread(){
        	@Override
        	public void run() {
        		while(true) {
        			long time = SystemClock.uptimeMillis();
					try {
						Thread.sleep(50);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
        		}
        	}
        };
        thread.start();
//        while(true) {
//        	try {
//				Thread.sleep(300);
//			} catch (InterruptedException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//        }
	}
	
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    	mPFpsHandler.setFPS();
    	testTextureManager.loadAllResource(gl);
    	

        gl.glDisable(GL10.GL_DITHER);
        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,
                GL10.GL_FASTEST);
		gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
		gl.glActiveTexture(GL10.GL_TEXTURE0);
        gl.glEnable(GL10.GL_BLEND);    
		gl.glEnable(GL10.GL_ALPHA_BITS);
		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
        gl.glClearColor(.5f, .5f, .5f, 1);
        gl.glShadeModel(GL10.GL_SMOOTH);
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

        gl.glTexEnvx(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE,
                GL10.GL_MODULATE);
        
    }

    public void onDrawFrame(GL10 gl) {
    	mPFpsHandler.setFPS();
    	if(!testTextureManager.isLoaded) return;
    	simplePlane.mTextureId = testTextureManager.getTextureId(TestTextureManager.MENU);
        /*
         * By default, OpenGL enables features that improve quality
         * but reduce performance. One might want to tweak that
         * especially on software renderer.
         */
        gl.glClearColor(0.5f, 0.5f, 0.5f, 0.5f);
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

        gl.glLoadIdentity();
    	gl.glOrthof(0.0f, 320.0f, 480.0f, 0.0f, -1.0f, 1.0f);    

        for(int i = 0; i < 20; i ++) {
        	gl.glPushMatrix();
        	gl.glTranslatef(0, i * 16, 0);
			for(int j = 0; j < 15; j ++) {
				gl.glTranslatef( + j * 16,0, 0);
				simplePlane.draw(gl);
				gl.glTranslatef( - j * 16,0, 0);
			}
        	gl.glTranslatef(0, -i * 16, 0);
        }
		
        square.draw(gl);

    }

    public void onSurfaceChanged(GL10 gl, int w, int h) {
        gl.glViewport(0, 0, w, h);

        /*
        * Set our projection matrix. This doesn't have to be done
        * each time we draw, but usually a new projection needs to
        * be set when the viewport is resized.
        */

        float ratio = (float) w / h;
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glFrustumf(-ratio, ratio, -1, 1, 3, 7);

    }
    int[] textureIds;
    private Context mContext;
    private Triangle mTriangle;
    private int mTextureID;
    private void enable2d(GL10 gl){    
    	gl.glMatrixMode(GL10.GL_PROJECTION);    
    	gl.glPushMatrix();    
    	gl.glLoadIdentity();    
    	gl.glOrthof(0.0f, 320.0f, 480.0f, 0.0f, -1.0f, 1.0f);    
    	gl.glMatrixMode(GL10.GL_MODELVIEW);    
    	gl.glPushMatrix();    
    	gl.glLoadIdentity();		
    	}	
    private void disable2d(GL10 gl){    
    	gl.glMatrixMode(GL10.GL_PROJECTION);    
    	gl.glPopMatrix();      
    	gl.glMatrixMode(GL10.GL_MODELVIEW);    
    	gl.glPopMatrix();			
    }
}

class Triangle {
    public Triangle() {

        // Buffers to be passed to gl*Pointer() functions
        // must be direct, i.e., they must be placed on the
        // native heap where the garbage collector cannot
        // move them.
        //
        // Buffers with multi-byte datatypes (e.g., short, int, float)
        // must have their byte order set to native order

        ByteBuffer vbb = ByteBuffer.allocateDirect(VERTS * 3 * 4);
        vbb.order(ByteOrder.nativeOrder());
        mFVertexBuffer = vbb.asFloatBuffer();

        ByteBuffer tbb = ByteBuffer.allocateDirect(VERTS * 2 * 4);
        tbb.order(ByteOrder.nativeOrder());
        mTexBuffer = tbb.asFloatBuffer();

        ByteBuffer ibb = ByteBuffer.allocateDirect(VERTS * 2);
        ibb.order(ByteOrder.nativeOrder());
        mIndexBuffer = ibb.asShortBuffer();

        // A unit-sided equalateral triangle centered on the origin.
        float[] coords = {
                // X, Y, Z
                -0.5f, -0.25f, 0,
                 0.5f, -0.25f, 0,
                 0.0f,  0.559016994f, 0
        };

        for (int i = 0; i < VERTS; i++) {
            for(int j = 0; j < 3; j++) {
                mFVertexBuffer.put(coords[i*3+j] * 2.0f);
            }
        }

        for (int i = 0; i < VERTS; i++) {
            for(int j = 0; j < 2; j++) {
                mTexBuffer.put(coords[i*3+j] * 2.0f + 0.5f);
            }
        }

        for(int i = 0; i < VERTS; i++) {
            mIndexBuffer.put((short) i);
        }

        mFVertexBuffer.position(0);
        mTexBuffer.position(0);
        mIndexBuffer.position(0);
    }

    public void draw(GL10 gl) {
        gl.glFrontFace(GL10.GL_CCW);
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mFVertexBuffer);
        gl.glEnable(GL10.GL_TEXTURE_2D);
        gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTexBuffer);
        gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, VERTS,
                GL10.GL_UNSIGNED_SHORT, mIndexBuffer);
    }

    private final static int VERTS = 3;

    private FloatBuffer mFVertexBuffer;
    private FloatBuffer mTexBuffer;
    private ShortBuffer mIndexBuffer;
    
   
}
